<template>
    <div>
        <el-card>

            <el-form :inline="true" :model="searchForm" class="search-form" label-width="80px">
                <el-form-item label="用户名">
                    <el-input v-model="searchForm.username" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="角色">
                    <el-input v-model="searchForm.role" placeholder="请输入角色" />
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="searchForm.mail" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="searchForm.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="searchForm.status" placeholder="请输入状态" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSearch">搜索</el-button>
                    <el-button @click="resetSearch">重置</el-button>
                </el-form-item>
            </el-form>


            <el-button type="primary" @click="handleAdd">新增用户</el-button>

            <el-table :data="users" style="width: 100%; margin-top: 20px">
                <el-table-column prop="id" label="ID" width="80" />
                <el-table-column prop="username" label="用户名" />
                <el-table-column prop="role" label="角色" />
                <el-table-column prop="mail" label="邮箱" />
                <el-table-column prop="phone" label="手机号" />
                <el-table-column prop="status" label="状态" />
                <el-table-column prop="createTime" label="创建时间" />
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <div style="display: flex; gap: 3px">
                            <el-button size="small" @click="copyUser(scope.row)">复制</el-button>
                            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
            <el-form :model="form" label-width="80px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username" />
                </el-form-item>
                <el-form-item label="角色">
                    <el-select v-model="form.role" placeholder="请选择角色">
                        <el-option label="管理员" value="admin" />
                        <el-option label="普通用户" value="user" />
                    </el-select>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.mail" />
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.phone" />
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="form.status" />
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input v-model="form.createTime" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveUser">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'UserManage',
    data() {
        return {
            users: [
                { id: 1, username: 'admin', role: 'admin', mail: '123@qq.com', phone: '12123123123123', status: '正常', createTime: '2025-01-01 12:00:00' },
                { id: 2, username: 'user01', role: 'user', mail: '123@qq.com', phone: '12123123123123', status: '正常', createTime: '2025-01-01 12:00:00' },
            ],
            dialogVisible: false,
            dialogTitle: '新增用户',
            form: {
                id: null,
                username: '',
                role: ''
            },
            searchForm: {
      username: '',
      role: '',
      mail: '',
      phone: '',
      status: ''
    }
        }
    },
    methods: {
        handleAdd() {
            this.dialogTitle = '新增用户'
            this.form = { id: null, username: '', role: '' }
            this.dialogVisible = true
        },
        handleEdit(row) {
            this.dialogTitle = '编辑用户'
            this.form = { ...row }
            this.dialogVisible = true
        },
        handleDelete(id) {
            this.$confirm('确认删除该用户？', '提示', {
                type: 'warning'
            }).then(() => {
                this.users = this.users.filter(user => user.id !== id)
                this.$message.success('删除成功')
            })
        },
        saveUser() {
            if (this.form.id) {
                // 编辑用户
                const index = this.users.findIndex(user => user.id === this.form.id)
                if (index !== -1) {
                    this.$set(this.users, index, { ...this.form })
                    this.$message.success('编辑成功')
                }
            } else {
                // 新增用户
                const newId = this.users.length ? Math.max(...this.users.map(u => u.id)) + 1 : 1
                this.users.push({ ...this.form, id: newId })
                this.$message.success('新增成功')
            }
            this.dialogVisible = false
        },
        copyUser(row) {
            const newId = this.users.length ? Math.max(...this.users.map(u => u.id)) + 1 : 1
            this.users.push({ ...row, id: newId })
            this.$message.success('新增成功')
        },
        handleSearch() {
            // 过滤逻辑在 computed 中，点击时会自动更新
        },
        resetSearch() {
            this.searchForm = {
                username: '',
                role: '',
                mail: '',
                phone: '',
                status: ''
            }
        }
    }
}
</script>